package com.huaguoguo.cyg.spider.pipeline;

import com.huaguoguo.cyg.converter.WBDTOConverter;
import com.huaguoguo.cyg.entity.weibo.dto.WbDetailDTO;
import com.huaguoguo.cyg.entity.weibo.dto.WbMediaDTO;
import com.huaguoguo.cyg.entity.weibo.to.WBDetail;
import com.huaguoguo.cyg.service.weibo.data.IWbDetailService;
import com.huaguoguo.cyg.service.weibo.data.IWbMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类说明： WeiboUserProcessor爬到的数据进行处理<br/>
 * author:huaguoguo
 * Date: 2019/8/16
 * Time: 23:15
 */
@Slf4j
@Component
public class WeiboUserPipeline implements Pipeline {

    @Autowired
    private IWbDetailService wbDetailService;

    @Autowired
    private IWbMediaService wbMediaService;
    /**
     * 处理爬虫数据
     *
     * @param resultItems resultItems
     * @param task        task
     */
    @Override
    public void process(ResultItems resultItems, Task task) {
        List<WBDetail> wbDetailList = (List<WBDetail>) resultItems.get("wbList");
        // 爬虫数据对象 转 数据库数据对象
        // 转WbDetailDTO
        List<WbDetailDTO> wbDetailDTOList = wbDetailList.stream()
                .map(WBDTOConverter.wbDetailDTOconvert()::apply)
                .collect(Collectors.toList());
        wbDetailService.saveBatch(wbDetailDTOList);
        // 转 WbMediaDTO
        List<WbMediaDTO> wbMediaDTOList = wbDetailList.stream()
                .flatMap(wbDetail -> WBDTOConverter.wbMediaDTOconvert().apply(wbDetail).stream())
                .collect(Collectors.toList());
        wbMediaService.saveBatch(wbMediaDTOList);
    }

}
